package Wormish.agiletre.se.ParticleSystem;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import Wormish.agiletre.se.Math.Vec2f;

public class Particle
{
	public float Scale = 1.0f;
	public float Rotation = 0;
	public Vec2f Position = Vec2f.Zero;
	public Vec2f Velocity = Vec2f.Zero;
	
	public float Alpha = 1.0f;
	public float LifeSpan = 1.0f;
	public float LifeTime = 0;
	
	public Bitmap Texture;
	public boolean IsActive = true;
	
	private static Paint particlePaint; // Used to modify the textures alpha
	
	public Particle()
	{
		if(particlePaint == null)
		{
			particlePaint = new Paint();
			particlePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
		}
	}
	
	public void Draw(Canvas canvas)
	{
		Matrix transforms = new Matrix();
		
		transforms.postTranslate(-Texture.getWidth() / 2, -Texture.getHeight() / 2); // <- Tt
		transforms.postScale(Scale, Scale);
		transforms.postRotate(Rotation); //<- R
		transforms.postTranslate(Position.getX(), Position.getY()); // <- Tp
	
		// Tt flyttar origo till mitten av bilden
		// M = Tp * R * S * Tt

		particlePaint.setAlpha((int)(Alpha * 255));

		//thePaint.
		
		canvas.drawBitmap(Texture, transforms, particlePaint);	
	}
	
	public void Update(float dt)
	{
		LifeTime += dt;
		
		Position.add(Velocity.scale(dt, true));
		
		Alpha = 1.0f - LifeTime / LifeSpan;
		
		if(LifeTime >= LifeSpan)
		{
			IsActive = false;
		}
	}
}
